package m.jclecai.com.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.fragment.news.NewsFragment;
import m.jclecai.com.view.common.RectangleTabLayout;

/**
 * 新闻资讯的activity
 * <p>
 * R.layout.activity_news
 */
public class NewsActivity extends BaseActivity  {

    @BindView(R.id.vp_view_pager)
    ViewPager mVpViewPager;

    @BindView(R.id.activity_news)
    LinearLayout mActivityNews;

    @BindView(R.id.rtl_indicator)
    RectangleTabLayout mRtlIndicator;

    private CharSequence[] mStrings = {"足彩", "篮彩", "数字彩"};
    private List<NewsFragment> mNewsFragments;


    @Override
    protected int getContentView() {
        return R.layout.activity_news;
    }

    @Override
    protected void initView() {

        leftOptionText("");
        setTitle("新闻资讯");

    }

    @Override
    protected void initData() {

        for (int i = 0; i < mStrings.length; i++) {

            NewsFragment newsFragment = new NewsFragment();
            newsFragment.setPosition(i);
            if(mNewsFragments==null){
                 mNewsFragments=new ArrayList<>();
            }
            mNewsFragments.add(newsFragment);
        }

        mVpViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        mRtlIndicator.setupWithViewPager(mVpViewPager);

       mNewsFragments.get(0).requestData("football");
    }


    @Override
    protected void initListener() {

        mVpViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(final int position) {
                String type="";
                if (position==0) {
                    type="football";
                }else if(position==1){
                    type="basketball";
                }else if(position==2){
                    type="digital";
                }
                try {
                    mNewsFragments.get(position).requestData(type);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }



    public class MyPagerAdapter extends FragmentPagerAdapter {

        @Override
        public CharSequence getPageTitle(int position) {
            return mStrings[position];
        }

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            return mNewsFragments.get(position);
        }

        @Override
        public int getCount() {
            return mStrings.length;
        }
    }

}
